package stegosaur.cli;

import stegosaur.Version;
import stegosaur.volume.*;


/**
 * <p>
 * Provides a very basic command-line interface to Stegosaur.  If "-h" or "--help" are specified, 
 * usage information is dumped to stdout.
 * </p>
 * <p>
 * Note: this interface is mostly for testing purposes and will either be removed
 * or greatly enhanced before the 1.0 release.
 * </p>
 *
 * @author gndn.k5
 * @since Stegosaur 1.0
 */
public final class CLI implements VolumeEventListener
{
  private Options options;
  private int currentProgress;
  
  private CLI(Options options) 
  {
    this.options = options;
  }

  private void execute()
  {    
    // Show usage if requested:
    if (options.showHelp)
    {
      showUsage();
      return;
    }
    
    // Create volume if requested:
    else if (options.createVolume)
    {
      currentProgress = 0;
      System.out.println("Creating volume \""+options.volumeParameters.name+"\" with master file \""+options.volumeParameters.masterFile.getAbsolutePath()+"\"");
      System.out.println();
      System.out.println("0%----------------------50%-----------------------100%");
      try
      {
        Volume volume = Volume.createVolume(options.volumeParameters, options.passphrase, this);
        System.out.println();
        System.out.println("Volume created!");
      }
      catch (Exception e)
      {
        System.out.println();
        System.out.println("Error: "+e.getMessage());
      }
    }
    
    // Unsure what was requested:
    else
    {
      System.out.println("Nothing to do: use -h or --help for usage.");
    }
  }
  
  
  /** Dumps usage info to stdout. **/
  private void showUsage()
  {
    System.out.println("USAGE: stegosaur [options]");
    System.out.println("OPTIONS:");
    System.out.println("  -h or --help : Show this help usage information");
    System.out.println("  -c or --create : Create a new volume");
    System.out.println();
    System.out.println("When creating volumes, the following options are accepted:");
    System.out.println("  --masterFile=<filename> : specifies the name of the volume's master file");
    System.out.println("  --volumeName=<name> : specifies the name of the new volume");
    System.out.println("  --splitSize=<size> : the maximum size, in MB, of the volume data files");
    System.out.println("       (volume data will be split across multiple files of this size)");
    System.out.println("       Set this to 0 to prevent splitting (all volume data in one file");
    System.out.println("  --splitNameFormat=<format> : the naming format to use for split files.");
    System.out.println("       Refer to the javadoc for more information.");
    System.out.println("  --blockSize=<size> : the size, in bytes, of each block.");
    System.out.println("  --capacity=<size> : the total desired volume capacity, in MB.");
    System.out.println("  --pass=<pass> : the passphrase which will encode the default layer.");
    System.out.println("       The passphrase must not include a space (this is a limitation of");
    System.out.println("       this command-line driver, not a limitation of Stegosaur itself).");
    System.out.println();
    System.out.println("For full documentation, refer to the Stegosaur javadocs.");
    System.out.println();
  }
  
  
  /** Inherited from VolumeEventListener: **/
  public boolean volumeFormatEvent(Volume src, int currentStep, int totalSteps)
  {
    int actualProgress = (int)((currentStep / (float)totalSteps) * 50);
    while (actualProgress > currentProgress)
    {
      System.out.print("=");
      currentProgress++;
    }
  
    return true;
  }
  
  
  /** Entry point. **/
  public static void main(String[] args)
  {
    // Hello world
    System.out.println(Version.NAME + " " + 
                       Version.MAJOR + "." + 
                       Version.MINOR + "." + 
                       Version.PATCH + " - " +
                       Version.URL);
    System.out.println();
      
    // Parse command line args:                 
    Options options = null;
    try
    {
      options = new Options(args);
    }
    catch (Exception e)
    {
      System.out.println("Error: "+e.getMessage());
      System.out.println("Use -h or --help for usage.");
      System.out.println();
      return;
    }
  
    // Execute requested function:
    new CLI(options).execute();
  }
}
